﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace FieldTemplate
{
    /// <summary>
    /// PhoneNumSet.xaml 的交互逻辑
    /// </summary>
    public partial class PhoneNumSet : UserControl
    {
        public PhoneNumSet()
        {
            InitializeComponent();
        }
    }

    public class PhoneNumFieldSetVm : FieldSetVmBase
    {
        public PhoneNumFieldSetVm()
        {
            FieldName = "电话号码";
            FieldType = "phonenum";

            SetPanel = new PhoneNumSet();
            SetPanel.DataContext = this;
            AreaCode = "0571";
            MobilePhoneNumPre = "137";
            MobilePhoneChecked = true;
            TelePhoneChecked = true;
        }

        Random rm = new Random();

        private string _AreaCode;
        public string AreaCode
        {
            get
            {
                return _AreaCode;
            }
            set
            {
                _AreaCode = value;
                RaisePropertyChanged("AreaCode");
            }
        }
        public string TelePhoneCode
        {
            get
            {
                string lastSix = rm.Next(1, 999999).ToString().PadLeft(6, '0');
                return AreaCode+ "-86"+lastSix ;
            }

        }
        public string MobilePhoneCode
        {
            get
            {
                string lastEight = rm.Next(1, 99999999).ToString().PadLeft(8, '0');
                return MobilePhoneNumPre + lastEight;

            }
        }

        private string _MobilePhoneNumPre;
        public string MobilePhoneNumPre
        {
            get
            {
                return _MobilePhoneNumPre;
            }
            set
            {
                _MobilePhoneNumPre = value;
                RaisePropertyChanged("MobilePhoneNumPre");
            }
        }

        private bool _MobilePhoneChecked;
        public bool MobilePhoneChecked
        {
            get
            {
                return _MobilePhoneChecked;
            }
            set
            {
                _MobilePhoneChecked = value;
                RaisePropertyChanged("MobilePhoneChecked");
            }
        }

        private bool _TelePhoneChecked;
        public bool TelePhoneChecked
        {
            get
            {
                return _TelePhoneChecked;
            }
            set
            {
                _TelePhoneChecked = value;
                RaisePropertyChanged("TelePhoneChecked");
            }
        }

        public override string GetFieldValue()
        {
            if(TelePhoneChecked && MobilePhoneChecked)
            {
                //如果都勾上，手机号码产生的概率要比电话号码多，这样合理一点
                int r = rm.Next(1, 5);
                if (r % 3 == 0)
                    return TelePhoneCode;
                else
                    return MobilePhoneCode;
            }else if(TelePhoneChecked && !MobilePhoneChecked)
            {
                return TelePhoneCode;
            }else if(!TelePhoneChecked && MobilePhoneChecked)
            {
                return MobilePhoneCode;
            }else
            {
                return "";
            }

        }

        public override Fieldinfo Serialize()
        {
            var tmp = base.Serialize();
            tmp.propertyinfos.Add(new Propertyinfo() { PorpertyName = "AreaCode", PropertyType = "string", PropertyValue = AreaCode.ToString() });
            tmp.propertyinfos.Add(new Propertyinfo() { PorpertyName = "MobilePhoneNumPre", PropertyType = "string", PropertyValue = MobilePhoneNumPre.ToString() });
            tmp.propertyinfos.Add(new Propertyinfo() { PorpertyName = "MobilePhoneChecked", PropertyType = "bool", PropertyValue = MobilePhoneChecked.ToString() });
            tmp.propertyinfos.Add(new Propertyinfo() { PorpertyName = "TelePhoneChecked", PropertyType = "bool", PropertyValue = TelePhoneChecked.ToString() });

            
            return tmp;
        }
    }

}
